//	Ordinaire.Tool.PagingCalculator
//	Copyright (c) 2008 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;

namespace Ordinaire.Tool
{
	/// <summary>
	/// PagingCalculator is a useful utility class to calculate the relevant paging detail.
	/// </summary>
	public class PagingCalculator
	{
		#region Variables declaration
		public static readonly int DEFAULT_PAGING_SIZE = 10;

		private static PagingCalculator _instance;

		private int _pagingSize;
		#endregion

		#region Constructor
		/// <summary>
		/// Creates new instance of PagingCalculator class.
		/// </summary>
		/// <remarks>default paging size is 10.</remarks>
		private PagingCalculator() : this(DEFAULT_PAGING_SIZE) { }

		/// <summary>
		/// Creates new instance of PagingCalculator class.
		/// </summary>
		/// <param name="size">paging size</param>
		private PagingCalculator(int size)
		{
			_pagingSize = size;
		}
		#endregion

		#region Public methods
		#region Singleton methods
		/// <summary>
		/// Creates new instance of PagingCalculator class.
		/// </summary>
		/// <param name="size">paging size</param>
		/// <returns>new instance of PagingCalculator class.</returns>
		/// <exception cref="System.ArgumentException">thrown when paging size is less than 1.</exception>
		public static PagingCalculator CreateInstance(int size)
		{
			if (size < 1)
			{
				throw new ArgumentException(
					String.Format(ExceptionMessage.MoreThanOneFormat, "size")
				);
			}

			if (_instance == null) { _instance = new PagingCalculator(size); }
			// resize if instance already exists
			else { _instance.PagingSize = size; }

			return _instance;
		}

		/// <summary>
		/// Creates new instance of PagingCalculator class.
		/// </summary>
		/// <remarks>default paging size is 10.</remarks>
		public static PagingCalculator Instance
		{
			get
			{
				if (_instance == null) { _instance = new PagingCalculator(); }
				return _instance;
			}
		}
		#endregion

		#region Property methods
		/// <summary>
		/// Sets paging size.
		/// </summary>
		/// <exception cref="System.ArgumentException">thrown when paging size is less than 1.</exception>
		public int PagingSize
		{
			get { return _pagingSize; }
			set
			{
				if (value < 1) { throw new ArgumentException("Paging size must be equal or more than 1."); }
				_pagingSize = value;
			}
		}
		#endregion

		/// <summary>
		/// Calculates paging size with the specified page number and total number of pages.
		/// </summary>
		/// <param name="pageNo">page number</param>
		/// <param name="totalPage">total number of items</param>
		/// <returns>paging size of the current page number.</returns>
		/// <exception cref="System.ArgumentNullException"></exception>
		public int GetSize(int pageNo, int total)
		{
			if (pageNo < 1)
			{
				throw new ArgumentException(
					String.Format(ExceptionMessage.MoreThanOneFormat, "pageNo")
				);
			}

			if (total < 1)
			{
				throw new ArgumentException(
					String.Format(ExceptionMessage.MoreThanOneFormat, "totalPage")
				);
			}

			int startIndex = GetStartIndex(pageNo);
			int totalSize = total - startIndex;
			if (totalSize < 1)
			{
				throw new ArgumentException(
					String.Format(ExceptionMessage.OutOfRangeFormat, "pageNo")
				);
			}

			return (totalSize > _pagingSize) ? _pagingSize : totalSize;
		}

		/// <summary>
		/// Calculates the start index of the current page.
		/// </summary>
		/// <param name="pageNo">page number</param>
		/// <returns>start index.</returns>
		/// <exception cref="System.ArgumentException">thrown when page number is less than 1.</exception>
		public int GetStartIndex(int pageNo)
		{
			if (pageNo < 1)
			{
				throw new ArgumentException(
					String.Format(ExceptionMessage.MoreThanOneFormat, "pageNo")
				);
			}

			return (pageNo - 1) * _pagingSize;
		}

		/// <summary>
		/// Calculates the total number of pages with the specified total items.
		/// </summary>
		/// <param name="total">total number of items</param>
		/// <returns>total number of pages.</returns>
		/// <exception cref="System.ArgumentException">thrown when total number of items is less than 1.</exception>
		public int GetTotalPage(int total)
		{
			if (total < 1)
			{
				throw new ArgumentException(
					String.Format(ExceptionMessage.MoreThanOneFormat, "total")
				);
			}

			return (int)Math.Ceiling((double)total / (double)_pagingSize);
		}
		#endregion
	}
}
